﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PlcReadDbInfoController : ControllerBase
    {
        private readonly ILogUtil _logger;
        private readonly PlcReadDbInfoApp _plcReadDbInfoApp;
        public PlcReadDbInfoController(ILogUtil logger, PlcReadDbInfoApp plcReadDbInfoApp)
        {
            _logger = logger;
            _plcReadDbInfoApp = plcReadDbInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<long>> AddPlcReadDbInfoAsync(PlcReadDbInfoDto PlcReadDbInfoDto)
        {
            try
            {
                long result = await _plcReadDbInfoApp.AddPlcReadDbInfoAsync(PlcReadDbInfoDto);
                return ApiBase.Success(result, "新增PlcReadDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<long>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<bool>> DeletePlcReadDbInfoAsync(long ID)
        {
            try
            {
                bool result = await _plcReadDbInfoApp.DeletePlcReadDbInfoAsync(ID);
                return ApiBase.Success<bool>("删除PlcReadDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> UpdatePlcReadDbInfoAsync(PlcReadDbInfoDto PlcReadDbInfoDto)
        {
            try
            {
                bool result = await _plcReadDbInfoApp.UpdatePlcReadDbInfoAsync(PlcReadDbInfoDto);
                return ApiBase.Success<bool>("修改PlcReadDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<PlcReadDbInfoDto>>> GetAllPlcReadDbInfoAsync(QueryPlcReadDbInfoDto queryPlcReadDbInfoDto)
        {
            try
            {
                var pageList = await _plcReadDbInfoApp.GetPlcReadDbInfoAsync(queryPlcReadDbInfoDto);
                return ApiBase.Info(pageList.Result, pageList.Total, "查询成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<PlcReadDbInfoDto>>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<PlcReadDbInfoDto>> GetPlcReadDbInfoByIDAsync(long ID)
        {
            try
            {
                var result = await _plcReadDbInfoApp.GetPlcReadDbInfoByIDAsync(ID);
                return ApiBase.Success(result, "获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<PlcReadDbInfoDto>(ex.Message);
            }
        }
    }
}
